package utils;

import javax.crypto.SecretKey;
import javax.servlet.*; 
import javax.servlet.http.*; 
import java.awt.*; 
import java.awt.image.*; 
import com.sun.image.codec.jpeg.*; 
import java.io.*; 
import java.util.Date; 
import java.text.SimpleDateFormat; 
 
public class ImageCounterServlet extends HttpServlet { 
    private static final float JPEG_QUALITY = (float) 0.85; 
 
    private String mStartDate; 
    private int mVisitCounter; 
 
    /** 
     * Called by the servlet container when the servlet is 
     * instantiated. Initializes the counter and start date. 
     */ 
    public void init() { 
        Date now = new Date(); 
        SimpleDateFormat dateFormatter = 
            new SimpleDateFormat("d.M.yyyy HH:mm:ss"); 
        mStartDate = dateFormatter.format(now); 
        mVisitCounter = 0; 
    } 
 
    /** 
     * Creates a graphical image and draws given text with 
     * with "Monospaced" yellow font on a red background. 
     */ 
    public BufferedImage createImage(String msg) { 
        Font font = new Font("Monospaced", Font.BOLD, 28); 
        FontMetrics fm = new Canvas().getFontMetrics(font); 
        int width = fm.stringWidth(msg) + 22; 
        int height = fm.getHeight() + 11; 
        BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB); 
        Graphics g = image.getGraphics(); 
        g.setColor(Color.gray); 
        g.fillRect(0, 0, width, height); 
        g.setColor(Color.yellow); 
        g.drawRoundRect(3, 3, width-7, height-7, 15, 15); 
        g.setFont(font); 
        g.setColor(Color.black); 
        g.drawString(msg, 11+2, fm.getAscent()+2); 
        g.setColor(Color.white); 
        g.drawString(msg, 11, 10 + fm.getAscent()); 
        g.setColor(Color.red);
        g.drawLine(0, 10, width, 30);
        g.drawLine(0, 30, width, 10);
        g.drawLine(10, 20, width, 40);
        g.drawLine(20, 40, width, 20);
        return image; 
    } 
 
    /** 
     * Called by the servlet container on HTTP GET request. 
     * Increases the counter and sends as a response a JPEG 
     * image that contains the counter value along with some 
     * additional text. 
     */ 
    public void doGet(HttpServletRequest request, HttpServletResponse response) 
    throws IOException, ServletException { 
        String msg = request.getParameter("code"); 
        Object o = request.getSession().getAttribute("key");
        SecretKey key = (SecretKey)o;
        DesEncrypter encrypter = new DesEncrypter(key);
        msg = encrypter.decrypt(msg);
        BufferedImage image = createImage(msg); 
        response.setContentType("image/jpeg"); 
        OutputStream out = response.getOutputStream(); 
        JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out); 
        JPEGEncodeParam jpegParams = encoder.getDefaultJPEGEncodeParam(image); 
        jpegParams.setQuality(JPEG_QUALITY, false); 
        encoder.setJPEGEncodeParam(jpegParams); 
        encoder.encode(image); 
        out.close(); 
    } 
 
    /** 
     * Called by the servlet container on HTTP POST request. 
     * Just delegate to doGet() method. 
     */ 
    public void doPost(HttpServletRequest request, HttpServletResponse response) 
    throws IOException, ServletException { 
        doGet(request, response); 
    } 
}
